package com.vinobase.controller.administration;

import com.vinobase.controller.management.CountryForm;
import com.vinobase.model.bean.CountryLocal;
import com.vinobase.model.bean.Culture;
import com.vinobase.model.bean.PersonalCountry;
import com.vinobase.model.dao.CountryDao;
import com.vinobase.model.dao.CultureDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.security.Principal;
import java.util.Locale;
import java.util.SortedSet;

@Controller
@RequestMapping(value="/administration/country")
public class CountryController {

    @Autowired
    private CultureDao cultureDao;

    @Autowired
    private CountryDao dao;

    @ModelAttribute("countries")
    public SortedSet<CountryForm> populateCountries(Principal user, Locale locale) {
        return dao.getList(user,  locale);
    }

    @ModelAttribute("cultures")
    public SortedSet<Culture> populateCultures() {
        return cultureDao.getList();
    }

    @RequestMapping(value="/create.html", method=RequestMethod.GET)
    protected String getCreateForm(Model model){
        model.addAttribute("country", new CountryForm());
        return "administration/country/create";
    }

    @RequestMapping(value="/list.html")
    protected String getList() {
        return "administration/country/list";
    }


    @RequestMapping(value="/create.html", method=RequestMethod.POST)
    protected String create(@Valid CountryForm country, BindingResult result, Principal user, Model model, Locale locale){
        if (result.hasErrors()) {
                    return "administration/country/create";
        }
        PersonalCountry pc = new PersonalCountry();
        if(country.getId()!=null)
            pc.setId(country.getId());
        pc.setName(country.getName());
        pc.setUserId(user.getName());
        dao.create(pc);
        model.addAttribute("countries",dao.getList(user, locale));
        return "administration/country/list";
    }

    @RequestMapping(value="/{id}.html", method=RequestMethod.GET)
    protected String view(@PathVariable Long id, Model model, Principal user){
        model.addAttribute("country",dao.get(id, user));
        return "administration/country/create";
    }



    @RequestMapping(value="/deletePersonal.html", method=RequestMethod.GET)
    protected String deletePersonal(HttpServletRequest request, Principal user, Model model, Locale locale){
        dao.deletePersonal(Long.parseLong(request.getParameter("id")), user);
        model.addAttribute("countries",dao.getList(user, locale));
        return "administration/country/list";
    }






}
